package com.bookstore.services.Impl;

import com.bookstore.mappers.UserMapper;
import com.bookstore.pojo.User;
import com.bookstore.services.UserService;
import com.bookstore.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {

    @Override
    public boolean registUser(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        try {
           if(user.getUsername()==null&&user.getPassword()==null&&user.getEmail()==null){
               return false;
           }
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.insert(user)>0;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }return false;
    }

    /**
     * 登录
     * @param user
     * @return
     */
    public User login(User user){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        try {
            return mapper.selectUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }return null;
    }

    @Override
    public List<User> selectUsers() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        try {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            SqlSessionUtil.close(sqlSession);
        }
        return null;
    }


}
